/*=============================================================================

  BACKGROUND MANAGER MODULE

  FILE: BKmgr.cpp
  
  DESCRIPTION

    Manager Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include "BKmgr_MOD.h"

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/

/*=============================================================================
  COMMAND HANDLER
=============================================================================*/
void BKmgr::CMD_Alloc(void)
{
	bkSET(BKmgr_NONE_M);
	m_logFS = m_errFS = NULL;
}

void BKmgr::CMD_Free(void)
{
	if(m_logFS)
	{
		FileOp_FileFindClose(m_logFS);
		m_logFS = NULL;
	}
	if(m_errFS)
	{
		FileOp_FileFindClose(m_errFS);
		m_errFS = NULL;
	}
	bkSET(BKmgr_NONE_M);
}

void BKmgr::Command_Handler(dword cmd, dword wp, dword lp)
{
	switch(cmd)
	{
	case BKmgr_LOG_C:
	{
		CMD_LOG();
		break;
	}
	case BKmgr_ERR_C:
	{
		CMD_ERR();
		break;
	}
	}
}

//-----------------------------------------------
// LOG
//-----------------------------------------------
static void logDELETE(char *root, char *fname, Time_s *tm, boolean err);
void BKmgr::CMD_LOG(void)
{
	char			*fname;
	int32		j;

	fname = (char*)memCalloc(128);
	if(ISMASKON(m_bkMASK, BKmgr_LOG_M) == FALSE)
	{
		int64		tm_val;
		if(m_logFS)
		{
			FileOp_FileFindClose(m_logFS);
			m_logFS = NULL;
		}
		DBGPRINT("[CMD_LOG] START.TO.DELETE\n");
		tm_val = Time_GetTime();
		Time_GetLocalTime(tm_val, &m_logTODAY);
		m_logFS = FileOp_FileFindFirst(UIstring_TBL(UISTR_LOG_PATH), fname, 128);
		if(m_logFS != NULL)
		{
			logDELETE(UIstring_TBL(UISTR_LOG_PATH), fname, &m_logTODAY, FALSE);
			bkON(BKmgr_LOG_M);
			REQ_LOG();
		}
		memFree(fname);
		return;
	}
	if(m_logFS == NULL)
	{
		bkOFF(BKmgr_LOG_M);
		memFree(fname);
		return;
	}
	//---CHECK&DELETE.IN.4-DAYS.UNIT
	for(j=0; j<4; j++)
	{
		if(FileOp_FileFindNext(m_logFS, fname, 128) == FALSE)
		{
			FileOp_FileFindClose(m_logFS);
			m_logFS = NULL;
			bkOFF(BKmgr_LOG_M);
			memFree(fname);
			DBGPRINT("[CMD_LOG] FINISH.TO.DELETE\n");
			return;
		}
		logDELETE(UIstring_TBL(UISTR_LOG_PATH), fname, &m_logTODAY, FALSE);
	}
	REQ_LOG(); // CONTINUE.ON
	memFree(fname);
}

void BKmgr::CMD_ERR(void)
{
	char			*fname;
	int32		j;

	fname = (char*)memCalloc(128);
	if(ISMASKON(m_bkMASK, BKmgr_ERR_M) == FALSE)
	{
		int64		tm_val;
		if(m_errFS)
		{
			FileOp_FileFindClose(m_errFS);
			m_errFS = NULL;
		}
		DBGPRINT("[CMD_ERR] START.TO.DELETE\n");
		tm_val = Time_GetTime();
		Time_GetLocalTime(tm_val, &m_errTODAY);
		m_errFS = FileOp_FileFindFirst(UIstring_TBL(UISTR_LOGERR_PATH), fname, 128);
		if(m_errFS != NULL)
		{
			logDELETE(UIstring_TBL(UISTR_LOGERR_PATH), fname, &m_errTODAY, TRUE);
			bkON(BKmgr_ERR_M);
			REQ_ERR();
		}
		memFree(fname);
		return;
	}
	if(m_errFS == NULL)
	{
		bkOFF(BKmgr_ERR_M);
		memFree(fname);
		return;
	}
	//---CHECK&DELETE.IN.4-DAYS.UNIT
	for(j=0; j<4; j++)
	{
		if(FileOp_FileFindNext(m_errFS, fname, 128) == FALSE)
		{
			FileOp_FileFindClose(m_errFS);
			m_errFS = NULL;
			bkOFF(BKmgr_ERR_M);
			memFree(fname);
			DBGPRINT("[CMD_ERR] FINISH.TO.DELETE\n");
			return;
		}
		logDELETE(UIstring_TBL(UISTR_LOGERR_PATH), fname, &m_errTODAY, TRUE);
	}
	REQ_ERR(); // CONTINUE.ON
	memFree(fname);
}

#define LOG_SAVE_DAY			(31)
void logDELETE(char *root, char *fname, Time_s *tm, boolean err)
{
	int32		j, y, m, d;
	char			*kname;
	boolean		keepIT;

	if((strcmp(fname, ".") == 0) || (strcmp(fname, "..") == 0))
		return;

	kname = (char*)memCalloc(128);
	keepIT = FALSE;
	y = tm->nYear;
	m = tm->nMonth;
	d = tm->nDay;
	for(j=0; j<LOG_SAVE_DAY; j++)
	{
		if(err)
		{
			sprintf(kname, "ERR_%00004d%002d%002d.log", y, m, d);
		}
		else
		{
			sprintf(kname, "LOG_%00004d%002d%002d.log", y, m, d);
		}
		if(strcmp(kname, fname) == 0)
		{
			keepIT = TRUE;
			break;
		}
		d -= 1;
		if(d <= 0)
		{
			m -= 1;
			if(m <= 0)
			{
				y -= 1;
				m = 12;
			}
			switch(m)
			{
			case 1: case 3: case 5: case 7: case 8: case 10: case 12:
			{
				d = 31;
				break;
			}
			case 2:
			{
				d = 29;
				break;
			}
			default:
			{
				d = 30;
				break;
			}
			}
		}
		//DBGPRINT("%s\n", flist[i]);
	}
	if(keepIT == FALSE)
	{
		sprintf(kname, "%s\\%s", root, fname);
		DBGPRINT("DELETE<%s>\n", kname);
		DeleteFile(kname);
	}
	memFree(kname);
}

/*--- END OF BKmgr_CMD.CPP ---*/
